package com.ciko.code.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.ciko.code.bean.AttendanceRecordBaen;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.HttpClient;
import com.ciko.code.http.HttpResponseListener;
import com.ciko.code.http.Req.AttendanceRecordReq;
import com.ciko.code.utils.NetWorkUtils;
import com.ciko.code.utils.Util;
import com.ciko.code.views.pullrefresh.PullToRefreshBase;
import com.ciko.code.views.pullrefresh.PullToRefreshListView;
import com.mywork.R;
import com.mywork.activity.AttendanceDetails;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by Administrator on 2016/4/2.
 */
public class AttendanceRecordFragment extends Fragment implements HttpResponseListener {

    private PullToRefreshListView list_view;
    private List<AttendanceRecordBaen> list;
    private List<AttendanceRecordBaen> datas;
    private AttendanceRecordAdapter attenAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.attendance_record_fragment, null);

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        list_view = (PullToRefreshListView) getView().findViewById(R.id.list_view);
        // headView = LayoutInflater.from(getActivity()).inflate(R.layout.attendance_record_fragment, null);
        //list_view.getRefreshableView().addHeaderView(headView);

        // attenAdapter = new AttendanceRecordAdapter();


        list_view.getRefreshableView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(getActivity(), AttendanceDetails.class));

            }
        });

        doHttpRequst(0, 10);


        list_view.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                //下拉刷新
                //doRequestHttp(new AttendanceRecordReq(0, 20));
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                //上拉加载
                // doRequestHttp(new AttendanceRecordReq((long) SharedPreferencesUtils.get(getActivity(), "lastCreateTime", null), 20));

            }
        });

    }

    private String getDateHead(long curr) {
        Date date = new Date(curr);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    private String getDateContent(long curr) {
        Date date = new Date(curr);
        SimpleDateFormat format = new SimpleDateFormat("hh:mm");
        return format.format(date);
    }

    @Override
    public void onRequestStart(String tag) {

    }

    @Override
    public void onResponseSuccess(BaseResponse<?> response, String tag) {
        list = (List<AttendanceRecordBaen>) response.getReturnObject();
        datas = new ArrayList<AttendanceRecordBaen>();
        List<String> l = new ArrayList<String>();
        for (AttendanceRecordBaen ar : list) {

            l.add(getDateHead(ar.getCreateTime()));

        }
        Set set = new LinkedHashSet<String>();
        set.addAll(l);
        l.clear();
        l.addAll(set);

        AttendanceRecordBaen arb;
        for (int i = 0; i < l.size(); i++) {
            String head = l.get(i);
            arb = new AttendanceRecordBaen();
            arb.setType(0);
            arb.setDateTitle(head);
            datas.add(arb);
            for (int j = 0; j < list.size(); j++) {

                if (getDateHead(list.get(j).getCreateTime()).contains(head)) {
                    arb = new AttendanceRecordBaen();
                    arb.setType(1);
                    arb.setVisitCause(list.get(j).getVisitCause());
                    arb.setCreateTime(list.get(j).getCreateTime());
                    arb.setVisitCause(list.get(j).getVisitCause());
                    arb.setCustAddr(list.get(j).getCustAddr());
                    arb.setCustName(list.get(j).getCustName());
                    arb.setVisitTime(list.get(j).getVisitTime());
                    datas.add(arb);
                } else {
                    continue;
                }

            }
        }

//        for (int i = 0; i < list.size(); i++) {
//            if (l.get(i).equals(getDateHead(list.get(i).getCreateTime()))) {
//              AttendanceRecordBaen rb=new AttendanceRecordBaen();
//
//            }else
//            {
//
//            }
//        }

        for (int i = 0; i < l.size(); i++) {
            Util.showToast(getActivity(), "请求成功" + "---" + l.get(i));
        }
        list_view.getRefreshableView().setAdapter(new AttendanceRecordAdapter());
    }

    @Override
    public void onResponseFail(String tag, int resultCode) {
        if (NetWorkUtils.isConnected(getActivity()))
            Util.showToast(getActivity(), "加载失败，请重试");
    }

    class AttendanceRecordAdapter extends BaseAdapter {

        @Override
        public int getItemViewType(int position) {
            return datas.get(position).getType();
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public int getCount() {
            if (datas != null && datas.size() > 0)
                return datas.size();
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return datas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public boolean areAllItemsEnabled() {
            return false;
        }

        @Override
        public boolean isEnabled(int position) {
            if (datas.get(position).getType() == 0)
                return false;
            return super.isEnabled(position);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolderTitle holderTitle;
            ViewHolderContnet holderContent;
            int type = getItemViewType(position);
            if (convertView == null) {
                switch (type) {
                    case 0:
                        //显示头部布局
                        convertView = LayoutInflater.from(getActivity()).inflate(R.layout.recird_activity_title, null);
                        holderTitle = new ViewHolderTitle();
                        holderTitle.txt_title_date = (TextView) convertView.findViewById(R.id.txt_title_date);
                        holderTitle.txt_title_date.setText(getDateHead(datas.get(position).getCreateTime()));

                        convertView.setTag(holderTitle);
                        break;

                    case 1:
                        //显示内容布局
                        convertView = LayoutInflater.from(getActivity()).inflate(R.layout.record_activity_content, null);
                        holderContent = new ViewHolderContnet();
                        holderContent.txt_content_date = (TextView) convertView.findViewById(R.id.txt_content_date);
                        holderContent.txt_content = (TextView) convertView.findViewById(R.id.txt_content);
                        holderContent.txt_content_date.setText(getDateContent(datas.get(position).getVisitTime()));
                        holderContent.txt_content.setText(datas.get(position).getCustAddr());
                        convertView.setTag(holderContent);
                        break;
                }
            } else {
                switch (type) {
                    case 0:
                        holderTitle = (ViewHolderTitle) convertView.getTag();
                        holderTitle.txt_title_date.setText(getDateHead(datas.get(position).getCreateTime()));
                        break;

                    case 1:
                        holderContent = (ViewHolderContnet) convertView.getTag();
                        holderContent.txt_content_date.setText(getDateContent(datas.get(position).getVisitTime()));
                        holderContent.txt_content.setText(datas.get(position).getCustAddr());
                        break;
                }
            }
            return convertView;
        }
    }

    class ViewHolderTitle {
        TextView txt_title_date;
    }

    class ViewHolderContnet {
        TextView txt_content_date;
        TextView txt_content;
    }

    private void doHttpRequst(long lastCreateTime, int length) {
        HttpClient.getInstance(getActivity()).post(new AttendanceRecordReq(lastCreateTime, length), this);
    }

}
